<template>
  <div>
    <el-dialog
      title="配置权限"
      :visible.sync="buttontype"
      @close="fun(tname)"
      @open="getrole"
    >
      <div v-if="checkedIds && treeData">
        <el-tree
          :data="treeData"
          show-checkbox
          node-key="id"
          :default-expanded-keys="checkedIds"
          :default-checked-keys="checkedIds"
          :props="defaultProps"
          ref="getree"
        >
        </el-tree>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="upuser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { http, setauthority, setsavePermisson } from "../../api/api";
export default {
  props: ["type", "fun", "tname", "tablerow"],
  data() {
    return {
      buttontype: this.type, //模态框状态
      checkedIds: "", //当前角色已有的权限数组
      treeData: "", //权限树
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    upuser() {
      var arr = [
        ...this.$refs.getree.getCheckedKeys(),
        ...this.$refs.getree.getHalfCheckedKeys(),
      ];

      console.log(arr);

      // 配置权限发送请求
      this.$http
        .post(
          http + setsavePermisson,
          {
            // 角色id
            roleId: this.tablerow.id,
            permissions: arr.toString(),
          },
          { emulateJSON: true }
        )
        .then(
          (data) => {
            console.log(data.data);
            if (data.data.msg == "成功") {
              this.buttontype = false;
              this.$message({
                showClose: true,
                message: "恭喜你，设置角色成功",
                type: "success",
              });
            } else {
              this.$message.error(data.data.message);
            }
          },
          (error) => {
            window.location = "./login.html";
            this.$message.error(error.data.message);
          }
        );
    },
    //  获取角色信息
    getrole() {
      this.$http
        .get(http + setauthority, { params: { roleId: this.tablerow.id } })
        .then(
          (data) => {
            if (data.data.msg == "成功") {
              console.log(data.data);
              // 权限树
              this.treeData = data.data.data.treeData;
              // 当前角色有的权限
              this.checkedIds = data.data.data.checkedIds;
            } else {
              this.$message.error(data.data.data);
            }
          },
          (err) => {
            this.$message.error(err.data.message);
          }
        );
    },
  },
  watch: {
    type() {
      this.buttontype = this.type;
    },
    /*  tablerow() {
      var json = {};
      for (var i in this.tablerow) {
        this.$set(json, i, this.tablerow[i].toString());
      }
      this.form = json;
    }, */
    /*  tablerow() {
      this.form.rolelds = this.tablerow.roleid;
    }, */
  },
};
</script>

<style>
</style>